home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1349 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: locking
  5. Date: Wed, 10 Jan 1996 17:30:59 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <30F3E9C3.15FB7483@intellektik.informatik.th-darmstadt.de>
  8. References: <4d0j6r$1ri@daphne.ecmwf.int>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Baudouin Raoult wrote:
  16. > I have an interesting problem. I would like to create an object
  17. > in shared memory, and lock/unlock it when accessing it:
  18. > class foo {
  19. > public:
  20. >         void bar();
  21. > };
  22. > void main()
  23. > {
  24. >         SharedObject<foo> fooH;
  25. >         fooH->bar();
  26. > }
  27. > When calling foo::bar, a lock must be set and reset. Using the
  28. > operator-> does not help, because I cannot write
  29. > SharedObject::operator->()
  30. > {
  31. >         lock();
  32. >         return object;
  33. >         unlock();
  34. > }
  35. > because the unlock is never executed. I solved my problem by adding
  36. > a temporary object:
  37. > SharedObject::operator->()
  38. > {
  39. >         return LockObject(object);
  40. > }
  41. > with
  42. > LockObject::LockObject(o) : object(o)
  43. > {
  44. >         lock();
  45. > }
  46. > LockObject::~LockObject()
  47. > {
  48. >         unlock();
  49. > }
  50. > LockObject::operator->()
  51. > {
  52. >         return object;
  53. > }
  54. > This seems to work, but the LockObject is only destroyed at the
  55. > end of the block, locking my object for too long.
  56. > main()
  57. > {
  58. >         SharedObject<foo> fooH("lock");
  59. >         ... the lock is not set
  60. >         fooH->bar();
  61. >         .. the lock is set until the end.
  62. > }
  63. > Anyone got an idea ?
  64.  
  65. I would say the temporary object should be destroyed directly after
  66. the invocation of 'bar'.
  67. As a workarround you can put the member-function call in brackets, e.g.
  68.  
  69.             { foo->bar(); }
  70.  
  71.     Enno
  72.